{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "#coding:utf-8\n",
    "%reload_ext autoreload \n",
    "%autoreload 2\n",
    "import os\n",
    "import sys\n",
    "\n",
    "project_basedir = '..'\n",
    "sys.path.append(project_basedir)\n",
    "from cchess_zero import cbf\n",
    "from cchess_zero import board_visualizer\n",
    "from matplotlib import pyplot as plt\n",
    "import os\n",
    "from cchess_zero.gameboard import *\n",
    "from net import resnet\n",
    "import common\n",
    "from gameplays import gameplay,players\n",
    "from gameplays.analysis import get_km_json\n",
    "import json\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-08-25_22-41-37_810_mcts-mcts_net-net_b.cbf\r\n",
      "2018-08-25_22-41-52_626_mcts-mcts_net-net_peace.cbf\r\n",
      "2018-08-25_22-42-14_923_mcts-mcts_net-net_w.cbf\r\n",
      "2018-08-25_22-42-28_909_mcts-mcts_net-net_w.cbf\r\n",
      "2018-08-25_22-42-30_907_mcts-mcts_net-net_b.cbf\r\n",
      "2018-08-25_22-42-39_253_mcts-mcts_net-net_w.cbf\r\n",
      "2018-08-25_22-42-43_552_mcts-mcts_net-net_w.cbf\r\n",
      "2018-08-25_22-43-01_131_mcts-mcts_net-net_w.cbf\r\n",
      "2018-08-25_22-43-31_266_mcts-mcts_net-net_w.cbf\r\n",
      "2018-08-25_22-44-03_804_mcts-mcts_net-net_b.cbf\r\n"
     ]
    }
   ],
   "source": [
    "! ls '../data/distributed/' | tail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2018-08-25_22-43-01_131_mcts-mcts_net-net_w.cbf\n"
     ]
    }
   ],
   "source": [
    "\n",
    "fullpath = '../data/distributed/2018-08-25_22-43-01_131_mcts-mcts_net-net_w.cbf'\n",
    "moves = cbf.cbf2move(fullpath)\n",
    "fname = fullpath.split('/')[-1]\n",
    "print(fname)\n",
    "state = gameplay.GameState()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import imageio\n",
    "imgs_for_gif = []\n",
    "state = gameplay.GameState()\n",
    "statestr = 'RNBAKABNR/9/1C5C1/P1P1P1P1P/9/9/p1p1p1p1p/1c5c1/9/rnbakabnr'\n",
    "for i in moves[:]: \n",
    "    statestr = GameBoard.sim_do_action(i,statestr)\n",
    "    imgs_for_gif.append(board_visualizer.get_board_img(statestr))\n",
    "    state.do_move(i)\n",
    "imgs_for_gif = imgs_for_gif[:]\n",
    "imageio.mimsave('../data/gif/{}.gif'.format(fname[:-4]), imgs_for_gif,duration=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('e9f9', 104)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i,len(moves)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def display_gif(fn):\n",
    "    from IPython import display\n",
    "    return display.HTML('<img src=\"{}\">'.format(fn))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=\"../data/gif/2018-08-25_22-43-01_131_mcts-mcts_net-net_w.gif\">"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "display_gif('../data/gif/{}.gif'.format(fname[:-4]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# ANALYSIS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "[None, 10, 9, 128]\n",
      "[None, 10, 9, 128]\n",
      "[None, 10, 9, 128]\n",
      "[None, 10, 9, 128]\n",
      "[None, 10, 9, 128]\n",
      "[None, 10, 9, 128]\n",
      "[None, 10, 9, 128]\n",
      "inside res net [None, 10, 9, 128]\n",
      "INFO:tensorflow:Restoring parameters from ../data/prepare_weight/2018-08-17_11-13-25\n"
     ]
    }
   ],
   "source": [
    "network = resnet.get_model(os.path.join(project_basedir,\n",
    "    '../data/prepare_weight/2018-08-17_11-13-25'[3:]),\n",
    "    common.board.create_uci_labels(),GPU_CORE=[None],FILTERS=128,NUM_RES_LAYERS=7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "network_player = players.NetworkPlayer(state.currentplayer,network,n_playout=400)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('c0e2', 0.15995199868339108)"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "network_player.make_move(state=state,actual_move=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.15995199868339108"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "network_player.mcts_policy._root._Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a3a4 0 3.845509e-05 0\n",
      "b0a0 0 0.00014557115 0\n",
      "b0b1 0 0.0014822551 0\n",
      "b0b2 0 0.00090064644 0\n",
      "b0b3 0 0.002988217 0\n",
      "b0b4 0 0.005231117 0\n",
      "b0b5 0 0.0024759679 0\n",
      "b0b6 15 0.03290768 0.9476261337598165\n",
      "b0b7 103 0.217501 0.9991115539976694\n",
      "b0b8 1 0.012088603 0.9533587098121643\n",
      "b0b9 0 0.00024749892 0\n",
      "c2a1 0 4.3057804e-05 0\n",
      "c2e3 64 0.13091888 0.9910937855020163\n",
      "c3c4 11 0.023087915 0.9850160587917675\n",
      "d5b4 0 6.904853e-05 0\n",
      "d5b6 0 0.0014900416 0\n",
      "d5e3 0 0.0048407777 0\n",
      "d5f4 0 0.0009169835 0\n",
      "d5f6 0 0.0024604101 0\n",
      "e0f0 0 0.0016041754 0\n",
      "g4g5 0 0.0006683134 0\n",
      "g8a8 0 0.0063022277 0\n",
      "g8b8 0 0.011707161 0\n",
      "g8c8 13 0.015473258 0.9907070260781509\n",
      "g8d8 8 0.01288397 0.9813164174556732\n",
      "g8e8 0 0.0066475626 0\n",
      "g8f8 18 0.037054785 0.9892583423190646\n",
      "g8h8 0 0.0070561916 0\n",
      "g8i8 0 0.00878184 0\n",
      "g8g5 9 0.013060891 0.9886536002159119\n",
      "g8g6 14 0.013397759 0.9766642962183271\n",
      "g8g7 33 0.0649443 0.9887887925812694\n",
      "g8g9 60 0.12346473 0.9871647278467811\n",
      "h9e9 0 0.001378259 0\n",
      "h9g9 0 0.0018126551 0\n",
      "h9i9 13 0.012728269 0.9928401112556458\n",
      "h9h0 0 0.00228861 0\n",
      "h9h1 0 0.0025898525 0\n",
      "h9h2 0 0.0076173805 0\n",
      "h9h3 11 0.022975499 0.9875629652630199\n",
      "h9h4 0 0.0034087668 0\n",
      "h9h5 0 0.0016543132 0\n",
      "h9h6 0 0.010044425 0\n",
      "h9h7 13 0.025699653 0.9960218301186196\n",
      "h9h8 0 0.0077133705 0\n",
      "i3i4 13 0.026660077 0.9840547763384305\n",
      "e1f2 0 2.5737077e-06 0\n",
      "e1d2 0 6.6593515e-07 0\n",
      "e1f0 0 2.1828271e-05 0\n",
      "c0e2 0 0.0016658362 0\n",
      "g0i2 0 0.00014635017 0\n",
      "c0a2 0 5.5201384e-05 0\n",
      "g0e2 0 0.0018876407 0\n"
     ]
    }
   ],
   "source": [
    "childs = network_player.mcts_policy._root._children\n",
    "for move in childs:\n",
    "    val = childs[move]\n",
    "    print(move,val._n_visits,val._P,val._Q)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "datajson = get_km_json(network_player.mcts_policy,state.statestr,depth=40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "with open('../data/mstc_vis/sample.km','w') as whdl:\n",
    "    json.dump(datajson,whdl)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf1.3_python",
   "language": "python",
   "name": "tf1.3_kernel"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
